package com.ms.pda.activity

import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.GsonUtils
import com.ms.architecture.base.DataBindingConfig
import com.ms.pda.BR
import com.ms.pda.R
import com.ms.pda.base.ScanActivity
import com.ms.pda.messenger.SubmitFailedMessenger
import com.ms.pda.model.Constants
import com.ms.pda.model.KeyMap
import com.ms.pda.model.vo.SubmitFailedVO
import kotlinx.coroutines.launch

class SubmitFailedActivity : ScanActivity() {

    private var mState: SubmitFailedViewModel? = null
    private var mMsger: SubmitFailedMessenger? = null

    override fun initViewModel() {
        mState = getActivityScopeViewModel(SubmitFailedViewModel::class.java)
        mMsger = getActivityScopeViewModel(SubmitFailedMessenger::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_submit_failed, BR.state, mState!!)
            .addBindingParam(BR.msger, mMsger!!)
            ?.addBindingParam(BR.instance, this)
    }

    override fun initViews() {
        super.initViews()
        val from = intent.extras?.getInt(Constants.FROM_KEY, -1)!!
        mState!!.from.set(from)
        when (from) {
            KeyMap.CODE1.value -> {
                mState!!.title.set(getString(R.string.bindCode1Failed))
            }
            KeyMap.CODE2.value -> {
                mState!!.title.set(getString(R.string.bindCode2Failed))
            }
            KeyMap.CODE3.value -> {
                mState!!.title.set(getString(R.string.bindCode3Failed))
            }
            KeyMap.DISMANTLE_CODE2.value -> {
                mState!!.title.set(getString(R.string.dismantle_code2_submit_failed_title))
            }
            KeyMap.DISMANTLE_CODE3.value -> {
                mState!!.title.set(getString(R.string.dismantle_code3_submit_failed_title))
            }
            KeyMap.MATCHING_PACKAGE.value -> {
                mState!!.title.set(getString(R.string.matchingPackageSubmitFailed))
            }
            KeyMap.SECONDARY.value -> {
                mState!!.title.set(getString(R.string.secondaryPackageSubmitFailed))
            }
            KeyMap.RETURNING.value -> {
                mState!!.title.set(getString(R.string.returnDataCommitFailed))
            }
            KeyMap.WAREHOUSING.value -> {
                mState!!.title.set(getString(R.string.warehousingDataCommitFailed))
            }
            KeyMap.OUT_OF_STOCK.value -> {
                mState!!.title.set(getString(R.string.outOfStockDataCommitFailed))
            }
        }
    }

    override fun loadData() {

    }

    override fun handleResult(code: String) {
        if (!mMsger!!.checkCodeExist(code)) {
            checkCodeError(getString(R.string.error_code_not_exist_in_list))
            return
        }
        lifecycleScope.launch {
            when (mState!!.from.get()) {
                KeyMap.CODE1.value, KeyMap.CODE2.value, KeyMap.CODE3.value -> {
                    mMsger!!.deleteBindCode(this@SubmitFailedActivity, code)
                }
                KeyMap.DISMANTLE_CODE2.value, KeyMap.DISMANTLE_CODE3.value -> {
                    mMsger!!.deleteDismantleCode(this@SubmitFailedActivity, code)
                }
                KeyMap.MATCHING_PACKAGE.value -> {
                    mMsger!!.deleteMatchingCode(this@SubmitFailedActivity, code)
                }
                KeyMap.SECONDARY.value -> {
                    mMsger!!.deleteSecondaryCode(this@SubmitFailedActivity, code)
                }
                KeyMap.WAREHOUSING.value, KeyMap.OUT_OF_STOCK.value -> {
                    mMsger!!.deleteStorageCode(this@SubmitFailedActivity, code)
                }
                KeyMap.RETURNING.value -> {
                    mMsger!!.deleteReturnCode(this@SubmitFailedActivity, code)
                }
            }
        }
    }

    class SubmitFailedViewModel : ViewModel() {
        val title: ObservableField<String> = ObservableField("")
        val from: ObservableInt = ObservableInt(-1)
    }
}